<?php
class Statistic_Model_Usersonline extends Zend_Db_Table{
	protected $_name = 'Users_online';
	protected $_primary = 'visitor';
	public  function delete_session($time)
	{// co dung
		$adapter= $this->getDefaultAdapter();
		try {
			$t=$time * 60;
			$a= mktime();
			$kt=$a-$t;
			$adapter->beginTransaction();
			$where= $adapter->quoteInto('lastvisit < ?',$kt);
			$adapter->delete('users_online',$where);
			$adapter->commit();
			
		} 
		catch (Zend_Db_Table_Exception $e)
		{
			$adapter->rollBack();
			return -1;
		}
		return 1;
	}
	public function update_session ($uo_ip,$uo_time,$uo_user,$data)
	{// co dung
		$select=$this->select();
		$select->from ('users_online',array('visitor'))
				->setIntegrityCheck(false)
				-> where('visitor= ?',$uo_ip);
		$arr= $this->fetchAll($select);
		$count=count($arr);
		
		if($count!=0)
		{
		$adapter=$this->getDefaultAdapter();
		try {
			$adapter->beginTransaction();
			$where=$adapter->quoteInto('visitor =?',$data['visitor']);
			$adapter->update('users_online', array('visitor'=>$data['visitor'],'lastvisit'=>$data['lastvisit'],'username'=>$data['username']),$where);
			$adapter->commit();
		}
		catch (Zend_Db_Table_Exception $e)
		{
			$adapter->rollBack();
			return -1;
		}
		}
		else 
		{
		
		
		$adapter = $this->getDefaultAdapter();
		try {
			$adapter->beginTransaction();
			$adapter->insert('users_online', $data);
			$adapter->commit();
		}
		catch (Zend_Db_Table_Exception $e) {
			$adapter->rollBack();
			return -1;
		}
		return 1;
		}
		
		
		
	}
	public function count ()
		{// co dung
			$select=$this->select();
			$select->from ('users_online')	
				->setIntegrityCheck(false);
			$arr= $this->fetchAll($select);
			$kq=count($arr);
			return  $kq;
			
		}

}